layui.use(['table', 'layer', 'admin', 'ax', 'func','laydate','formSelects', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 运营组-广告栏管理
     */
    var NanSellerAdvert = {
        tableId: "nanSellerAdvertTable"
    };

    var enumAjax = new $ax(Feng.ctxPath + "/enums/checkStatus");
    var enumRes = enumAjax.start();
    var projects = enumRes.data;
    var html = "";
    for(var key in projects){
        html += '<option value="'+key+'">'+projects[key]+'</option>'
    }
    $("#checkStatus").append(html);
    form.render('select');

    /**
     * 初始化表格的列
     */
    NanSellerAdvert.initColumn = function () {
        return [[
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 150},
            {field: 'sellerId', sort: false, title: '运营组ID'},
            {field: 'sellerName', sort: false, title: '运营组'},
            {field: 'deviceName', sort: false, title: '设备'},
            {field: 'image', sort: false, title: '图片', templet: '<div><img src="{{Feng.cdn}}{{d.image}}" alt="" /></div>'},
            {field: 'link', sort: false, title: '跳转地址'},
            {field: 'name', sort: false, title: '备注'},
            {field: 'sort', sort: true, title: '排序'},
            {field: 'checkStatusStr', sort: false, title: '审核状态'},
            {field: 'cancelReason', sort: false, title: '审核拒绝理由'},
            {field: 'isDel', sort: false, title: '是否删除', templet: '<div>{{d.isDel? "是":"否"}}</div>'},
            {field: 'createTime', sort: false, title: '创建时间'},
            {field: 'updateTime', sort: false, title: '更新时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanSellerAdvert.search = function () {
        var queryData = {};

        queryData['sellerId'] = $('#sellerId').val();
        queryData['isDel'] = $('#isDel').val();
        queryData['checkStatus'] = $('#checkStatus').val();

        table.reload(NanSellerAdvert.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    NanSellerAdvert.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/nanSellerAdvert/add'
    };

    window.reloadPage = function(){
        table.reload(NanSellerAdvert.tableId);
    }

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    NanSellerAdvert.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/nanSellerAdvert/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    NanSellerAdvert.exportExcel = function () {
        var checkRows = table.checkStatus(NanSellerAdvert.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellerAdvert.onDeleteItem = function (data) {
        admin.open({
            id: 'cancelBox',
            type: 2,
            title: '提交拒绝理由',
            shade: 0,
            content: Feng.ctxPath + '/jump/cancelBox?id=' + data.id + "&u=nanSellerAdvert"
        });
    };

    NanSellerAdvert.onEditPage = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellerAdvert/editItem", function (data) {
                Feng.success("操作成功!");
                table.reload(NanSellerAdvert.tableId);
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanSellerAdvert.tableId,
        url: Feng.ctxPath + '/nanSellerAdvert/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanSellerAdvert.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanSellerAdvert.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    NanSellerAdvert.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        NanSellerAdvert.exportExcel();
    });

    table.on('sort('+NanSellerAdvert.tableId+')', function(obj){
            table.reload(NanSellerAdvert.tableId, {
                initSort: obj,
                where: {
                    field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase()
                    ,order: obj.type
                }
            });
        });

    // 工具条点击事件
    table.on('tool(' + NanSellerAdvert.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanSellerAdvert.onEditPage(data);
        } else if (layEvent === 'delete') {
            NanSellerAdvert.onDeleteItem(data);
        }
    });
});
